//
//  String+Ex.swift
//  APP
//
//  Created by Rdxer on 2017/12/14.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import Foundation

/// from https://qa.1r1g.com/sf/ask/1696065381/
public extension Array  where Element == Int{
    func toHex(full_0:Bool = true,len:Int=2,lowerCase:Bool=true,divider:String = "")->String{
        
        var str = ""
        
        for v in self {
            str += v.toHex(full_0: full_0, len: len, lowerCase: lowerCase)
            str += divider
            
        }
        
        return str
        
    }
}
public extension Int{
    func toHex(full_0:Bool = true,len:Int=2,lowerCase:Bool=true) -> String {
        
        var formatstr = ""
        
        formatstr.append("%")
        
        if full_0 {
            formatstr.append("0")
            
        }
        formatstr.append(len.description)
        
        if lowerCase {
            formatstr.append("x")
        }else{
            formatstr.append("X")
        }
        
        var st = String(format:formatstr, self)
        
        return st;
    }
    func toString(full_0:Bool = true,len:Int=2) -> String {
        
        var formatstr = ""
        
        formatstr.append("%")
        
        if full_0 {
            formatstr.append("0")
        }
        formatstr.append(len.description)
        
//        if lowerCase {
            formatstr.append("d")
//        }else{
//            formatstr.append("X")
//        }
        
        var st = String(format:formatstr, self)
        
        return st;
    }
}
public extension String{
    
//    var  hexi:UInt = UInt(hexS, radix: 16)
    
    func hexToInt() -> Int? {
        return Int.init(self, radix: 16) ?? nil
    }
    
    
    func toInt() -> Int? {
        return Int.init(self) ?? nil
    }
    
}
